// 4.0 获取菜单数据
import axios from "../../utils/axios"

export default {
    namespaced: true,
    state() {
        return {
            menulist: [],
            parentList:[]
        }
    },
    mutations: {
        saveMenuListMutations(state, list) {
            console.log("saveMenuListMutations执行了")
            console.log(list)
            state.menulist = list
        },
        saveParentMenuListMutations(state,list){
            state.parentList = list
        }
    },
    actions: {
        getMenuListActions({commit}) {
            console.log("getMenuListActions执行了")
            axios({
                method: 'GET',
                url: '/api/menulist'
            }).then( response => {
                // 关联上下级菜单数据
                console.log(response)
                let pmenus = response.list.filter(item => item.pid === 0)
                pmenus.forEach(pitem => {
                    pitem.children = []
                    response.list.forEach(item => {
                        if (item.pid === pitem.id) {
                            pitem.children.push(item)
                        }
                    })
                })

                commit('saveMenuListMutations', pmenus)
            })
        },
        getParentMenuListActions({commit}) {
            axios({
                method: "GET",
                url: "/api/menulist"
            }).then( response => {
                if (response.code === 200) {
                    let parentList = response.list.filter(item => item.pid === 0)
                    console.log(parentList, "getParentMenusListActions")
                    commit('saveParentMenuListMutations', parentList)
                }
            })
        },
        postMenuActions({commit, dispatch}, form) {
            let url = form.id === "" ? '/menuadd' : "/menuedit"
            axios({
                method: "POST",
                url: url,
                data: form
            }).then(response => {
                if (response.code === 200) {
                    // 菜单处理成功，更新菜单数据
                    dispatch('getMenuListActions')
                    dispatch('getParentMenusListActions')
                }
            })
        },
        deleteMenuActions({commit, dispatch}, id) {
            axios({
                method: 'POST',
                url: '/menudelete',
                data: {id}
            }).then(response=> {
                if (response.code === 200) {
                    dispatch('getMenuListActions')
                    dispatch('getParentMenusListActions')
                }
            })
        }
    },
    getters: {}
}


axios({
    method: 'GET',
    url: '/api/menulist'
}).then( response => {
    // 关联上下级菜单数据
    console.log(response,"你好menu")
    console.log(response.list,"menu   list")
    let pmenus = response.list.filter(item => item.pid === 0)
    pmenus.forEach(pitem => {
        pitem.children = []
        response.list.forEach(item => {
            if (item.pid === pitem.id) {
                pitem.children.push(item)
            }
        })
    })
})
